﻿package com.valorin.arenas;

import static com.valorin.Dantiao.getInstance;
import static com.valorin.configuration.DataFile.pd;
import static com.valorin.configuration.DataFile.ranking;
import static com.valorin.configuration.DataFile.records;
import static com.valorin.configuration.DataFile.saveRecords;
import static com.valorin.configuration.DataFile.savepd;
import static com.valorin.configuration.languagefile.MessageSender.sm;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import com.valorin.ranking.Ranking;


public class RecordData {
  public static void record(String arenaName,String winner,String loser,boolean isDraw) {
	Arena arena = getInstance().getArenasHandler().getArena(arenaName);
	int time = arena.getTime();
	
	
	double player1Damage = arena.getDamage(true);
	double player1MaxDamage = arena.getMaxDamage(true);
	double player2Damage = arena.getDamage(false);
	double player2MaxDamage = arena.getMaxDamage(false);
	
	double winnerDamage,winnerMaxDamage,loserDamage,loserMaxDamage;
	if (arena.isp1(winner)) {
	  winnerDamage = player1Damage;
	  winnerMaxDamage = player1MaxDamage;
	  loserDamage = player2Damage;
	  loserMaxDamage = player2MaxDamage;
	} else {
	  winnerDamage = player2Damage;
	  winnerMaxDamage = player2MaxDamage;
	  loserDamage = player1Damage;
	  loserMaxDamage = player1MaxDamage;
	}
	Player w = Bukkit.getPlayerExact(winner);
	Player l = Bukkit.getPlayerExact(loser);
	
	Calendar cal = Calendar.getInstance(); 
	SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String mDateTime = formatter.format(cal.getTime());
	
	int loserWin = records.getInt(loser + ".Win");
	int loserLose = records.getInt(loser + ".Lose");
	int loserDraw = records.getInt(loser + ".Draw");
	int loserGameTimes = loserWin + loserLose + loserDraw; 
	records.set(loser +".Record" + "." + loserGameTimes + ".player", winner);
    records.set(loser +".Record" + "." + loserGameTimes + ".time", time);
	records.set(loser +".Record" + "." + loserGameTimes + ".date", mDateTime);
	records.set(loser +".Record" + "." + loserGameTimes + ".damage", loserDamage);
	records.set(loser +".Record" + "." + loserGameTimes + ".maxdamage", loserMaxDamage);
	records.set(loser +".Record" + "." + loserGameTimes + ".isWin", false);
	if (isDraw) {
	  records.set(loser +".Record" + "." + loserGameTimes + ".isDraw", true);
	} else {
	  records.set(loser +".Record" + "." + loserGameTimes + ".isDraw", false);
	}

	int winnerWin = records.getInt(winner + ".Win");
	int winnerLose = records.getInt(winner + ".Lose");
	int winnerDraw = records.getInt(winner + ".Draw");
	int winnerGameTimes = winnerWin + winnerLose + winnerDraw;
	records.set(winner +".Record" + "." + winnerGameTimes + ".player", loser);
	records.set(winner +".Record" + "." + winnerGameTimes + ".time", time);
	records.set(winner +".Record" + "." + winnerGameTimes + ".date", mDateTime);
	records.set(winner +".Record" + "." + winnerGameTimes + ".damage", winnerDamage);
    records.set(winner +".Record" + "." + winnerGameTimes + ".maxdamage", winnerMaxDamage);
    if (isDraw) {
	  records.set(winner +".Record" + "." + winnerGameTimes + ".isWin", false);
	  records.set(winner +".Record" + "." + winnerGameTimes + ".isDraw", true);
	} else {
	  records.set(winner +".Record" + "." + winnerGameTimes + ".isWin", true);
	  records.set(winner +".Record" + "." + winnerGameTimes + ".isDraw", false);
	  double reward = getInstance().getConfig().getDouble("Rewards.Points");
	  pd.set(winner+".Points", pd.getDouble(winner+".Points") + reward);
	  savepd();
	  sm("&b做的不错！奖励你 &d{points} &b点单挑积分",w,"points",new String[]{""+reward});
	}
    
    if (isDraw) {
  	  records.set(winner +".Draw", winnerDraw + 1);
  	  records.set(loser +".Draw", loserDraw + 1);
  	} else {
  	  records.set(winner +".Win", winnerWin + 1);
  	  records.set(loser +".Lose", loserLose + 1);
  	}
    
    saveRecords();
    getInstance().getDansHandler().load();
    
    if (isDraw) {
      return;
    }
    
      Ranking r = getInstance().getRanking();
      int winnerRank = r.getWin(w);
	  int loserRank = r.getWin(l);
	  int winnerRank2 = r.getKD(w);
	  int loserRank2 = r.getKD(l);
	  
	  int winnerOrder = 0;
	  if (ranking.getStringList("Win") != null) {
		for (int i = 0;i < ranking.getStringList("Win").size();i++) {
	      if (ranking.getStringList("Win").get(i).split("\\|")[0]
	    		  .equals(winner)) {
	        winnerOrder = i;
	      }
		}
	  }
	  int loserOrder = 0;
	  if (ranking.getStringList("Win") != null) {
		for (int i = 0;i < ranking.getStringList("Win").size();i++) {
	      if (ranking.getStringList("Win").get(i).split("\\|")[0]
	    		  .equals(loser)) {
	    	loserOrder = i;
	      }
		}
	  }
	  
	  if (winnerOrder <= loserOrder) {//winner本来就强过loser 等号则代表没有排行数据，winner优先
	    r.rank(winner+"|"+records.getInt(winner+".Win"),true);
	    r.rank(loser+"|"+records.getInt(loser+".Win"),true);
	  } else {
		r.rank(loser+"|"+records.getInt(loser+".Win"),true);
	    r.rank(winner+"|"+records.getInt(winner+".Win"),true);
	  }
	  
	  int winnerOrder2 = 0;
	  if (ranking.getStringList("KD") != null) {
		for (int i = 0;i < ranking.getStringList("KD").size();i++) {
	      if (ranking.getStringList("KD").get(i).split("\\|")[0]
	    		  .equals(winner)) {
	        winnerOrder2 = i;
	      }
		}
	  }
	  int loserOrder2 = 0;
	  if (ranking.getStringList("KD") != null) {
		for (int i = 0;i < ranking.getStringList("KD").size();i++) {
	      if (ranking.getStringList("KD").get(i).split("\\|")[0]
	    		  .equals(loser)) {
	        loserOrder2 = i;
	      }
		}
	  }
	  if (winnerOrder2 <= loserOrder2) {//winner本来就强过loser 等号则代表没有排行数据，winner优先
	    if (records.getInt(winner+".Lose") != 0) {
	      r.rank(winner+"|"+
	          ((double)records.getInt(winner+".Win")/
			  (double)records.getInt(winner+".Lose")),false);
	    } else {
	      r.rank(winner+"|"+
		  ((double)records.getInt(winner+".Win")),false);
	    }
	  
	    if (records.getInt(loser+".Lose") != 0) {
	      r.rank(loser+"|"+
	          ((double)records.getInt(loser+".Win")/
			  (double)records.getInt(loser+".Lose")),false);
	    } else {
	      r.rank(loser+"|"+
	      ((double)records.getInt(loser+".Win")),false);
	    }
	  } else {
		  if (records.getInt(loser+".Lose") != 0) {
			r.rank(loser+"|"+
		          ((double)records.getInt(loser+".Win")/
				  (double)records.getInt(loser+".Lose")),false);
		  } else {
			r.rank(loser+"|"+
		    ((double)records.getInt(loser+".Win")),false);
		  }
		  if (records.getInt(winner+".Lose") != 0) {
			r.rank(winner+"|"+
		          ((double)records.getInt(winner+".Win")/
				  (double)records.getInt(winner+".Lose")),false);
		  } else {
			r.rank(winner+"|"+
			((double)records.getInt(winner+".Win")),false);
		  }
	  }
	  
	  if (winnerRank != r.getWin(w) && r.getWin(w) != 0) {
		sm("&b胜场排名发生变更！&e{before}->{now}",w,"before now",
		   new String[]{
		   ""+winnerRank,
		   ""+r.getWin(w)});
	  }
	  if (loserRank != r.getWin(l) && r.getWin(l) != 0) {
		sm("&b胜场排名发生变更！&e{before}->{now}",l,"before now",
		   new String[]{
		   ""+loserRank,
		   ""+r.getWin(l)});
	  }
	  if (winnerRank2 != r.getKD(w) && r.getWin(w) != 0) {
		sm("&bKD排名发生变更！&e{before}->{now}",w,"before now",
		   new String[]{
		   ""+winnerRank2,
		   ""+r.getKD(w)});
	  }
	  if (loserRank2 != r.getKD(l) && r.getWin(l) != 0) {
		sm("&bKD排名发生变更！&e{before}->{now}",l,"before now",
		   new String[]{
		   ""+loserRank2,
		   ""+r.getKD(l)});
	  }
  }
}
